<?php        
    //引入核心
    require_once('source/lib/cooperation.php');    
    
    //引入语言包
    require_once('source/site/lang.php');
    
    //引入站点配置
    require_once('source/site/site.php');
        
    //引入路由操作
    list($file, $action) = $_site->get_rewrite(false);
    if(!$file) {
        exit('rewrite load exit.');
    }
    //记录来源
    $_SESSION['referer'] = $_SERVER['HTTP_REFERER'];
    //检查接口访问权限
    $api_action = request('h');
    //api_key_check($api_action);    
    require_once $file;
    
    $rtn = array('state'=>false);
    if(!$action || !$action_ary[$action] || !function_exists($action_ary[$action])){
        $rtn['code'] = 1001;
        api_echo_json($rtn);
    }
    //执行函数
    $action_ary[$action]();
    
    
    
    function api_key_check($action){
        $obj = new api();
        $appkey = request('appkey');
        $key = request('secret');
        $authod = request('authod');

        $result = $obj->check($appkey, $key, $action, $authod);
        if($result !== true){
            $msg = array('state'=>false);
            $lang = array(0=>'8005', 1=>'8006', 2=>'8007', 3=>'8008', 4=>'8009', 5=>'8010');
            $msg['code'] = $lang[$result];
            api_echo_json($msg);
        }
    }    
    

	function api_echo_json($ary){
		global $_site, $api_action;
		//ajax跨域请求  leiqianyong 2013-10-16
        $jsonp = request('callback'); 		
		
		if(!isset($ary['state'])){
			$ary['state'] = true;
		}
		if(!isset($ary['list'])){
			$ary['list'] = false;
		}
		


		$lang_config = $_site->data_config['lang'];
		if($ary['code']){
			$tmp = $lang_config[$ary['code']];
			$ary['codestr'] = $tmp ? $tmp : $ary['code'];
		}

        
		//ajax跨域请求  leiqianyong 2013-10-16		
        if($jsonp){
            echo  $jsonp.'('.json_encode($ary).')' ;            
            exit;         
        }			
	
        header("Content-type: application/json");
		echo json_encode($ary);


//$obj->api_logs($t2-$t1 . ':' . $api_action . ':' . date('Y-m-d H:i:s'));
        exit;
	}
	/**
	* 接口输出格式转换
	* @author zhufu 2013-10-09
	*
	* @param mixed $value
	* @param string $format
	*/
	function api_format_switch($value, $format, $key=''){
        if(!$format){
            return $value;
        }
        if(!is_array($format)){
            //是最终格式字符串
            if(!is_array($value)){

		switch($format){
			case 'numeric':
				$value =  $value + 0;
				break;
			case 'string':
				if(!strlen($value) || $value === 'null'){
					$value =  '';
	    			}
	                        else{
	                            $value .= '';
	                        }       
	                        break;
	                    case 'bool':
                        if($value ==='false' || !$value){
	                            $value = false;
	                        }
	                        else{
	                            $value = true;
	                        }                        
					break;
			}
                return $value;
            }
            if($key){
                if(!isset($value[$key])){
                    if($value[0]){
                        foreach($value as $k=>$v){
                            $value[$k] = api_format_switch($v, $format, $key);
                        }
                    }         
                    else{
                        $value[$key] = '';
                    }           
                    return $value;
                }
                else{
                $value[$key] = api_format_switch($value[$key], $format);
		return $value;
                }
            }
            //循环检查输出数组
            foreach($value as $k=>$v){
                $value[$k] = api_format_switch($v, $format, $key);
            }
            return $value;
        }
        //循环格式数组
        foreach($format as $k=>$v){
            //格式类型不是数组，直接转换格式
            if(!is_array($v)){
                $value = api_format_switch($value, $v, $k);
                continue;
            }
            if(!$value[$k]){
            //是数组，则进行递归处理
            foreach($v as $k2=>$v2){
                $value = api_format_switch($value, $v2, $k2);
            }            
                return $value;
            }
            $value[$k] = api_format_switch($value[$k], $v, $k);
        }
        return $value;
	}
    
    /**
    * 接口输出格式转换，升级版
    * @author zhufu 2014-04-09
    * 
    * @param mixed $value 输出的值
    * @param mixed $format 格式控制配置
    * @param string $key 指定要检查的 key
    */
    function api_format_switch_v2($value, $format, $key=''){
        //在调用前检查 $value 的 isset
        if(!$format){
            return false;
        }
        
        //是规则，直接处理数据格式
        if(!is_array($format)){
            $value_tmp = $key ? $value[$key] : $value;            
            switch($format){
                case 'numeric':
                    $value_tmp =  $value_tmp + 0;
                    break;
                case 'string':
                    if(strlen($value_tmp) < 1 || $value_tmp === null){
                        $value_tmp = '';
                    }
                    else{
                        $value_tmp .= '';
                    }
                    break;
                case 'bool':
                    if($value_tmp === 'false' || !$value_tmp){
                        $value_tmp = false;
                    }
                    else{
                        $value_tmp = true;
                    }
                    break;
                case 'array_string':
                    if(!$value_tmp){
                        $value_tmp = array();
                        break;
                    }
                    foreach($value_tmp as $k=>$v){
                        $value_tmp[$k] = api_format_switch_v2($v, 'string');
                    }
                    break;
                case 'array_numeric':
                    if(!$value_tmp){
                        $value_tmp = array();
                        break;
                    }
                    foreach($value_tmp as $k=>$v){
                        $value_tmp[$k] = api_format_switch_v2($v, 'numeric');
                    }
                    break;
                default:
                    exit('没有对应的检查类型'.$format.$value);
            }
            if($key){
                $value[$key] = $value_tmp;
            }
            else{
                $value = $value_tmp;
            }
            return $value;
        }
        //是规则数组，进行剥离
        foreach($format as $k=>$v){
            //没有对应的 key ，有可以是数组
            if(!$value){
                $value = array();
                //$value = api_format_switch_v2($value, 'array');
            }
            else if(isset($value[0])){
                foreach($value as $k2=>$v2){
                    if(isset($v2[0])){
                        //针对接口 api_album/album_list_day 多个自动数组 的处理
                        foreach($v2 as $k3=>$v3){
                            $value[$k2][$k3] = api_format_switch_v2($v3, $v, $k);
                        }
                    }
                    else{
                        $value[$k2] = api_format_switch_v2($v2, $v, $k);
                    }                    
                }
            }
            else{
                $value[$k] = api_format_switch_v2($value[$k], $v);
            }            
        }
        return $value;
  
    }
    
?>